import { Color3, Engine, DirectionalLight, Vector3, Mesh, Scene, ArcRotateCamera, PointLight, StandardMaterial, Texture, ShaderMaterial } from 'babylonjs'

// 灯光
export class Demo3 {
  private dom: HTMLCanvasElement
  private engine: Engine
  private scene: Scene
  private camera: ArcRotateCamera

  constructor(dom: HTMLCanvasElement) {
    this.dom = dom
    this.engine = new Engine(this.dom, true)  // 设置引擎
    this.scene = new Scene(this.engine)   // 创建场景
    this.camera = new ArcRotateCamera("Camera", 0, 0, 10, Vector3.Zero(), this.scene); // 创建相机
    this.camera.setPosition(new Vector3(-10, 10, 0));
    this.camera.attachControl(this.dom, false)  // 把相机放到画布

    const light0 = new PointLight("Omni0", new Vector3(0, 10, 0), this.scene);
    const light1 = new PointLight("Omni1", new Vector3(0, -10, 0), this.scene);
    const light2 = new PointLight("Omni2", new Vector3(10, 0, 0), this.scene);
    const light3 = new DirectionalLight("Dir0", new Vector3(1, -1, 0), this.scene);

    var material = new StandardMaterial("kosh", this.scene);
    var sphere = Mesh.CreateSphere("Sphere", 16, 3, this.scene);

    const lightSphere1 = Mesh.CreateSphere('ls1', 16, 0.5, this.scene)
    const lightSphere2 = Mesh.CreateSphere('ls2', 16, 0.5, this.scene)
    const lightSphere3 = Mesh.CreateSphere('ls3', 16, 0.5, this.scene)

    const mt1 = new StandardMaterial('red', this.scene)
    mt1.diffuseColor = new Color3(0, 0, 0);   // 球体扩散光源
    mt1.specularColor = new Color3(0, 0, 0);  // 球体镜面光源
    mt1.emissiveColor = new Color3(1, 0, 0);  // 球体反射光源
    lightSphere1.material = mt1

    const mt2 = new StandardMaterial('green', this.scene)
    mt2.diffuseColor = new Color3(0, 0, 0);
    mt2.specularColor = new Color3(0, 0, 0);
    mt2.emissiveColor = new Color3(0, 1, 0);
    lightSphere2.material = mt2

    const mt3 = new StandardMaterial('blue', this.scene)
    mt3.diffuseColor = new Color3(0, 0, 0);
    mt3.specularColor = new Color3(0, 0, 0);
    mt3.emissiveColor = new Color3(0, 0, 1);
    lightSphere3.material = mt3

    material.diffuseColor = new Color3(1, 1, 1);
    sphere.material = material;


    light0.diffuse = new Color3(1, 0, 0);  // 扩散光源
    light0.specular = new Color3(1, 0, 0); // 镜面光源

    light1.diffuse = new Color3(0, 1, 0);
    light1.specular = new Color3(0, 1, 0);

    light2.diffuse = new Color3(0, 0, 1);
    light2.specular = new Color3(0, 0, 1);

    light3.diffuse = new Color3(1, 1, 1);
    light3.specular = new Color3(1, 1, 1);


    var alpha = 0;

    this.scene.beforeRender = function () {
      light0.position = new Vector3(10 * Math.sin(alpha), 0, 10 * Math.cos(alpha));
      light1.position = new Vector3(10 * Math.sin(alpha), 0, -10 * Math.cos(alpha));
      light2.position = new Vector3(10 * Math.cos(alpha), 0, 10 * Math.sin(alpha));

      lightSphere1.position = light0.position;
      lightSphere2.position = light1.position;
      lightSphere3.position = light2.position;

      alpha += 0.01;
    };

    window.addEventListener('resize', () => {
      this.engine.resize();
    })
  }

  render() {
    // 引擎渲染场景
    this.engine.runRenderLoop(() => {
      this.scene.render()
    })
  }




}


